package MyStack;

public class MyArrayStack<T> {
    private Object[] elem;
    private int size;
    private int maxNum;
    public MyArrayStack() {
        this.maxNum = 10;
        this.elem = new Object[maxNum];
        this.size = 0;
    }
    public MyArrayStack(int maxNum) {
        this.maxNum = maxNum;
        this.elem = new Object[maxNum];
        this.size = 0;
    }
    public boolean isEmpty() {
        return this.size == 0;
    }
    public boolean isFull() {
        return this.size == this.maxNum;
    }
    public boolean push(T o) {
        if(this.isFull()) {
            return false;
        }
        this.elem[size++] = o;
        return true;
    }
    public T pop() {
        if(this.isEmpty()) {
            return null;
        }
        size--;
        Object tmp = this.elem[size];
        elem[size] = null;
        return (T)tmp;
    }
    public T peek() {
        if(this.isEmpty()) {
            return null;
        }
        return (T)this.elem[size-1];
    }
    public String toString() {
        StringBuilder str = new StringBuilder();
        for(int i = 0; i < this.size; i++) {
            str.append(this.elem[i] + " ");
        }
        return str.toString();
    }
}
